﻿/*
 * ********************************************************************* *
  
Copyright HLRS, University Stuttgart

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* ********************************************************************* *
*/

using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace SirLibrary
{
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class SirServiceImpl : ISirService
    {
        Dictionary<string, EndpointInformation> m_SirData = new Dictionary<string, EndpointInformation>();

        public SirServiceImpl() { }

        #region ISirService Members

        void ISirService.RegisterEndpoint(string virtualName, EndpointInformation endpointInformation)
        {
            if (string.IsNullOrEmpty(virtualName))
            {
                throw new ArgumentNullException("virtualName");
            }

            if (null == endpointInformation)
            {
                throw new ArgumentNullException("endpointInformation");
            }

            System.Console.Out.WriteLine("RegisterEndpoint(\"{0}\", \"{1}\"", virtualName, 
                endpointInformation.EndpointAddress.ToEndpointAddress().Uri.AbsoluteUri);

            m_SirData[virtualName] = endpointInformation;
        }

        EndpointInformation ISirService.GetEndpoint(string virtualName)
        {
            System.Console.Out.Write("GetEndpoint(\"{0}\")", virtualName); 
            
            if (!m_SirData.ContainsKey(virtualName))
            {
                System.Console.ForegroundColor = ConsoleColor.Red;
                System.Console.Error.WriteLine(" does not resolve!!!");
                System.Console.ResetColor();

                throw new ArgumentOutOfRangeException("virtualName",
                    String.Format("Virtual endpoint {0} not found", virtualName));
            }

            EndpointInformation ei = m_SirData[virtualName];

            System.Console.Out.WriteLine(" resolves to {0} \"{1}\"", ei.ServiceType.ToString(),
                ei.EndpointAddress.ToEndpointAddress().Uri.AbsoluteUri); 

            return ei;
        }

        #endregion
    }
}
